package com.superb.common.database.mybatis.expression;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.operators.relational.InExpression;

/**
 * 自定义IN表达式，自带表达式没有括号
 * @Author: ajie
 * @CreateTime: 2024-07-15 14:00
 */
public class SuperbInExpression extends InExpression {

    private String getLeftExpressionString() {
        return super.getLeftExpression() + (super.getOldOracleJoinSyntax() == ORACLE_JOIN_RIGHT ? "(+)" : "");
    }

    @Override
    public String toString() {
        StringBuilder statementBuilder = new StringBuilder();
        statementBuilder.append(getLeftExpressionString());

        statementBuilder.append(" ");
        if (super.isGlobal()) {
            statementBuilder.append("GLOBAL ");
        }
        if (super.isNot()) {
            statementBuilder.append("NOT ");
        }
        statementBuilder.append("IN (");
        statementBuilder.append(super.getRightExpression()).append(")");
        return statementBuilder.toString();
    }

    @Override
    public void setRightExpression(Expression rightExpression) {
        if (rightExpression instanceof SuperbExpressionList list) {
            list.setBrackets(false);
        }
        super.setRightExpression(rightExpression);
    }
}
